﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xms.Abp;

public static class MenuDefinition
{
    private static readonly List<MenuItem> Menus = new();
    public static readonly List<PermissionItem> PermissionTree = new();
    private static readonly Dictionary<string, PermissionItem> PermissionDictionary = new();

    public static readonly Dictionary<string, string> FunctionNames = new()
    {
        { "PageList", "列表" },
        { "Details", "详情" },
        { "Create", "创建" },
        { "Update", "更新" },
        { "Delete", "删除" },
        { "DeleteMany", "批量删除" },
        { "ToggleEnable", "启用/禁用" },
        { "Export", "导出" },
        { "Import", "导入" },
        { "SubmitReview", "提审" },
        { "Review", "审核" },
        { "SQL", "SQL查询" },
    };

    public static readonly Dictionary<string, string> FunctionIcons = new()
    {
        { "PageList", "ordered-list" },
        { "Details", "file-search" },
        { "Create", "plus-circle" },
        { "Update", "edit" },
        { "Delete", "delete" },
        { "DeleteMany", "close-square" },
        { "ToggleEnable", "switcher" },
        { "Export", "export" },
        { "Import", "import" },
        { "SubmitReview", "issues-close" },
        { "Review", "fund-view" },
        { "SQL", "console-sql" },
    };

    public static bool ContainsPermission(string moduleName)
    {
        return PermissionDictionary.ContainsKey(moduleName);
    }

    public static PermissionItem GetPermissionItem(string moduleName)
    {
        return PermissionDictionary[moduleName];
    }

    public static List<MenuItem> GetMenus(string[] modules, bool isAdmin)
    {
        if (isAdmin)
        {
            return Menus;
        }
        return Menus.Select(r => r.FilterByPermissions(modules)).Where(r => r != null).ToList();
    }

    public static void AddMenu(MenuItem menu, params string[] modulePath)
    {

    }
}

public class MenuItem
{
    public string Path { get; set; }
    public string Name { get; set; }
    public string Key { get; set; }
    public string Icon { get; set; }
    public string Permission { get; set; }
    public List<string> Functions { get; set; }
    public List<MenuItem> Children { get; set; }

    public MenuItem(string name, string key, string icon, string permission, string path, params string[] functions)
    {
        Path = path;
        Name = name;
        Key = key;
        Icon = icon;
        Permission = permission;
        Functions = functions.ToList();
    }

    public MenuItem(string name, string key, string icon, List<MenuItem> children)
    {
        Name = name;
        Key = key;
        Icon = icon;
        Children = children;
    }

    public MenuItem FilterByPermissions(string[] permissions)
    {
        if ((Children == null || Children.Count <= 0))
        {
            if (string.IsNullOrWhiteSpace(Permission) || permissions?.Contains(Permission) == true)
            {
                return this;
            }
            else
            {
                return null;
            }
        }
        else
        {
            var children = new List<MenuItem>();
            foreach (var child in Children)
            {
                var c = child.FilterByPermissions(permissions);
                if (c != null)
                {
                    children.Add(c);
                }
            }
            if (children.Count > 0)
            {
                var menu = new MenuItem(Name, Key, Icon, Permission, Path)
                {
                    Children = children
                };
                return menu;
            }
            return null;
        }
    }
}
